import { use_cache_store } from '@/store/cache'
import { useRoute } from 'vue-router'
import { watch } from 'vue'
// 获取路由缓存名单
export function useGetKeepAlive() {
  let cache = use_cache_store()
  let route = useRoute()
  watch(
    () => route.name,
    (to, from) => {
      // 这里实现 配置缓存的路由实现前进刷新后退缓存功能

      // 进入列表刷新。从详情返回时候需缓存
      resetKeepAlive('page-list', ['page-detail'])

      /**
       * 返回表格通用数据
       * @param cacheRouterName 缓存的目标路由
       * @param cacheRouterNextNames 缓存的目标路由的所有下级跳转路由名
       */
      function resetKeepAlive(cacheRouterName, cacheRouterNextNames) {
        // 路由本身是缓存的。这里模拟路由前进的场景。重置路由的缓存来实现重新加载的效果
        if (to === cacheRouterName && cacheRouterNextNames.every((fromName) => from != fromName)) {
          cache.reset(cacheRouterName)
        }
      }
    }
  )

  return cache
}
